package sim.ring.gui;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;

import sim.ring.technical.data.DatabaseConnection;

public class ConfigurationManager {
	
	private static Logger log = Logger.getLogger("sim.ring.gui.ConfigurationManager");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int result = 0;
		DatabaseConnection db = new DatabaseConnection();
		try {
			db.connectToDB("RingingSimDB");
			if (!db.checkForTable("GUI_CONFIG")) {
				result = db.issueNonQueryStatement(
						"CREATE TABLE gui_config (" +
						"	gui_area VARCHAR(255)," +
						"	config_key VARCHAR(255)," +
						"	config_value VARCHAR(3000) )",null);
				log.info(String.format("RC: %d", result));
			}
			ResultSet r1 = db.issueQueryStatement("SELECT COUNT(*) FROM gui_config",null);
			if (r1.next()) {
				int value = r1.getInt(1);
				log.info(String.format("%d entries retrieved", value));
				if (value == 0) {
					result = db.issueNonQueryStatement(
						"INSERT INTO gui_config VALUES ('RingingSimApp', 'GUILanguage', 'EN')", null);
					log.info(String.format("RC: %d", result));
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeDB();
		}
		

	}

}
